package com.finance.base.constant;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * 常量定义
 */
public class BaseConstants {
    private static final Logger logger = LoggerFactory.getLogger(BaseConstants.class);

    //此处定义异常码 对应异常描述见[/properties/errCode.properties]
    public static final String ERR_UNKOWN = "9999";
    public static final String ERR_SUCCESS = "0000";
    public static final String ERR_SESSION_TIMEOUT = "0001";
    public static final String ERR_INVALID_PARAMETER = "0002";
    public static final String ERR_ENCRYPT = "0003";
    public static final String ERR_UNKONW_USER = "0004";
    public static final String ERR_UNKONW_ROLE = "0005";
    public static final String ERR_DUPLICATE_DATA = "0006";
    public static final String ERR_NON_EXISTENT = "0007";
    public static final String ERR_EXIST_LOWER = "0008";
    public static final String ERR_COPYBEAN = "0009";
    public static final String ERR_UNKOWN_CONFIG = "0010";
    public static final String ERR_HTTPCLIENT = "0011";
    public static final String ERR_PARAMETER_IS_NULL = "0012";
    public static final String ERR_CANNOT_DELETE_SELF = "0013";
    public static final String ERR_PARAM_MISS = "0014";
    public static final String ERR_SUPPLIER_CODE_EXIST = "0015";
    public static final String ERR_SUPPLIERGROUP_CODE_EXIST = "0016";
    public static final String ERR_INIT_JSON_IS_NULL = "0017";
    public static final String ERR_TABLE_NAME_NOT_EXIST="0018";
    public static final String ERR_DUPLICATE_CODE = "0019";
    public static final String ERR_SUPPLIER_CODE_EXISTS = "0020";
    public static final String ERR_SUPPLIER_GROUP_IS_NULL = "0021";
    public static final String ERR_NETWORK = "0022";
    public static final String ERR_UNCOMITTESORDERS_EXISTS ="0023";
    public static final String ERR_TIME ="0024";
    public static final String ERR_PARAMS_NULL ="0025";
    public static final String ERR_RESULT_NULL ="0026";
    public static final String ERR_SQL_EXCUTE_NULL ="0027";
    public static final String ERR_EXPORT ="0028";
    public static final String ERR_FAIL ="0029";
    public static final String ERR_MENU_SORT_EXIST ="0030";

    public static final String SYSCONFIG_KEY_BACKEND_BASE_URL = "backend_baseUrl";
    public static final String SYSCONFIG_KEY_BACKEND_LOGIN_URL = "backend_loginUrl";
    public static final String SYSCONFIG_KEY_BACKEND_THIRDAUTH_URL = "backend_thirdAuthUrl";
    public static final String SYSCONFIG_KEY_TIMMER_BASE_URL = "timmer_baseUrl";

    public static final String PAKAGESIZE_TYPE_SMALL = "小";
    public static final String PAKAGESIZE_TYPE_BIG = "大";
    public static final String PAKAGESIZE_TYPE_MIDDLE = "中";

    public static final int ONE = 1;
    public static final int TWO = 2;
    public static final int ZERO = 0;

        /*
        public static final String SYSCONFIG_KEY_TIMMER_ALLTASK_URL = "timmer_alltask";
        public static final String SYSCONFIG_KEY_TIMMER_DBTASK_URL = "timmer_dbtask";
        public static final String SYSCONFIG_KEY_TIMMER_TASKSTATE_URL = "timmer_taskstate";
        public static final String SYSCONFIG_KEY_TIMMER_ADDTASKURL = "timmer_addtask";
        public static final String SYSCONFIG_KEY_TIMMER_ADDSCHEDULETASK_URL = "timmer_addscheduletask";
        public static final String SYSCONFIG_KEY_TIMMER_PAUSETASK_URL = "timmer_pausetask";
        public static final String SYSCONFIG_KEY_TIMMER_REMOVETASK_URL = "timmer_removetask";
        public static final String SYSCONFIG_KEY_TIMMER_SCHEDULETASK_URL = "timmer_scheduletask";
        */

    /**
     * 运单拉取接口相关
     */
    public static final String SYSCONFIG_KEY_CHUYAN_OMS_PULL = "chuyan_oms_pull";
    public static final String SYSCONFIG_KEY_CHUYAN_OMS_CALLBACK = "chuyan_oms_callback";
    public static final String SYSCONFIG_KEY_CHUYAN_OMS_DIGISTKEY = "chuyan_oms_digestKey";
    public static final String SYSCONFIG_KEY_CHUYAN_OMS_CHARSET = "chuyan_oms_charset";
    public static final String SYSCONFIG_KEY_REQUEST_NUMS = "request_nums";
    public static final String SYSCONFIG_KEY_REPEAT = "1";
    public static final String SYSCONFIG_KEY_NOREPEAT = "0";
    public static final String PULL_FIRST_TIME = "first";
    public static final BigDecimal THOUSAND = new BigDecimal(1000);
    public static final String CHARSET = "UTF-8";

    public static final String SYSCONFIG_KEY_CHUYAN_OMS_TRIGGER = "chuyan_oms_trigger";
    public static final String SYSCONFIG_KEY_CHUYAN_OMS_CALCULATE = "chuyan_oms_calculate";

    /**
     * 运单数据相关表前缀
     */
    public static final String SYSCONFIG_KEY_LOGISTICDATA_TABLE = "biz_logistics_";
    public static final String SYSCONFIG_KEY_LOGISTICERROR_TABLE = "biz_logistics_error_log_";
    public static final String SYSCONFIG_KEY_LOGISTICS_EXTEND_TABLE = "biz_logistics_extend_";


    public static final String FINANCE_FILE_PATH="finance";
    public static final String FINANCE_REPORT_FILE_PATH="report";

    /**
     * OSS对象存储相关
     */
    public static final String OSS_ENDPOINT = "ENDPOINT";
    public static final String OSS_ACCESS_ID = "ACCESSID";
    public static final String OSS_ACCESS_KEY = "ACCESSKEY";
    public static final String OSS_BUCKET_NAME = "BUCKETNAME";
    public static final String DEFAULT_EXPORT_FILE_SUFFIX = ".xlsx";
    public static final String FINANCE_FILE_SEPORATOR="/";
    public static final String FILE_URL="FILEURL";


    public static final int ROUND = 4;

    /**
     * 字段解析错误类型
     */
    public static final String ANALYZE_ERROR_TYPE_DIRECZTION = "DirectionHandler";
    public static final String ANALYZE_ERROR_TYPE_TEMPERATURE = "TemperatureHandler";
    public static final String ANALYZE_ERROR_TYPE_IMPORTSOURCE = "ImportsourceHandler";
    public static final String ANALYZE_ERROR_TYPE_SUPPLIER = "SupplierHandler";
    public static final String ANALYZE_ERROR_TYPE_POSTMAN = "PostmanHandler";
    public static final String ANALYZE_ERROR_TYPE_DELIVERYSITE = "DeliverySiteHandler";
    public static final String ANALYZE_ERROR_TYPE_ACCEPTORGANIZATION = "AcceptOrganizationHandler";
    public static final String ANALYZE_ERROR_TYPE_STATUS = "StatusHandler";
    public static final String ANALYZE_ERROR_TYPE_SCHEDULETYPE = "ScheduleTypeHandler";
    public static final String ANALYZE_ERROR_TYPE_CONSIGNEEADDRESS = "ConsigneeAddressHandler";


    /**
     * 计算规则匹配错误类型
     */
    public static final String COMPUTE_ERROR_TYPE_MAJORRULE = "MajorRuleHandler";
    public static final String COMPUTE_TYPE_RANGERULE = "RangeRuleHandler";
    public static final String COMPUTE_TYPE_NORULE = "ERROR_TYPE_NORULE";
    public static final String COMPUTE_TYPE_MORERULE = "ERROR_TYPE_NORULE";
    public static final String COMPUTE_TYPE_SAVEPULLDATA = "ERROR_TYPE_SAVEPULLDATA";


    /**
     * 导出数据excel默认sheet
     */
    public static final String USER_DATA_REPORT = "用户信息数据报表模板";

    // 接口返回码对照表
    private static final Map<String, String> errcodeMap = new HashMap();
    private static final Map<String, String> ossMap = new HashMap();

    static {
        InputStreamReader reader = null;
        try {
            reader = new InputStreamReader(BaseConstants.class.getResourceAsStream("/properties/errCode.properties"), "UTF-8");
            Properties codeProps = new Properties();
            codeProps.load(reader);
            Iterator<Entry<Object, Object>> it = codeProps.entrySet().iterator();
            while (it.hasNext()) {
                Entry<Object, Object> entry = it.next();
                String key = entry.getKey().toString();
                String value = entry.getValue().toString();
                try {
                    errcodeMap.put(key, value);
                } catch (Exception e) {
                    logger.error("配置的返回码格式有错误", e.getMessage());
                }
            }
            logger.info("返回码列表加载完成");
        } catch (Exception e) {
            logger.error("不能读取[/properties/errCode.properties]文件.");
            logger.error(e.getMessage(),e);
        }finally {
            try{
                if(reader != null) {
                    reader.close();
                }
            }catch (IOException ioe){
                logger.error(ioe.getMessage(),ioe);
            }
        }
        InputStreamReader ossReader =null;
        try {
            ossReader = new InputStreamReader(BaseConstants.class.getResourceAsStream("/properties/oss.properties"), "UTF-8");
            logger.info("ossReader:{}", ossReader);
            Properties ossProps = new Properties();
            ossProps.load(ossReader);
            Iterator<Entry<Object, Object>> ossIterator = ossProps.entrySet().iterator();
            while (ossIterator.hasNext()) {
                Entry<Object, Object> ossEntry = ossIterator.next();
                String ossKey = ossEntry.getKey().toString();
                String ossValue = ossEntry.getValue().toString();
                try {
                    ossMap.put(ossKey, ossValue);
                    logger.info("ossMap:{}",ossMap.toString());
                } catch (Exception e) {
                    logger.error("配置的返回码格式有错误", e);
                }
            }
            logger.info("返回码列表加载完成");
        } catch (Exception e) {
            logger.error("不能读取[/properties/oss.properties]文件.");
        }finally {
            try{
                if(ossReader != null) {
                    ossReader.close();
                }
            }catch (IOException ioe){
                logger.error(ioe.getMessage(),ioe);
            }
        }
    }

    /**
     * 通过返回码， 获取返回描述
     *
     * @param code 返回码
     * @return 返回对应描述信息
     */
    public static final String getErrorMessage(String code) {
        return errcodeMap.get(code) == null ? "" : errcodeMap.get(code);
    }

    /**
     * 通过key获取oss相关配置信息
     *
     * @param ossKey
     * @return
     */
    public static final String getOssProperties(String ossKey) {
        return ossMap.get(ossKey) == null ? "" : ossMap.get(ossKey);
    }
}